﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WineStore.DAL;
using PagedList;
using PagedList.Mvc;
using WineStore.Models;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Web.UI;
using StructureMap;


namespace WineStore.Controllers
{
    public class BannerController : Controller
    {

        // GET: /Banner/ShowBanner/
        [OutputCache(CacheProfile = "ShowBanner")]
        public ViewResult ShowBanner(int? page)
        {

            using (var db = new WineDbContext())
            {
                var banners = db.Banners
                    .OrderByDescending(b => b.BannerId);

                int pageSize = 10;
                int pageNumber = (page ?? 1);

                // Thiet lap thuoc tinh khi phan trang
                PagedListRenderOptions pro = new PagedListRenderOptions();
                pro.MaximumPageNumbersToDisplay = 5;
                pro.LinkToLastPageFormat = "Last";
                pro.DisplayLinkToLastPage = true;
                pro.LinkToFirstPageFormat = "First";
                pro.DisplayLinkToFirstPage = true;
                pro.LinkToNextPageFormat = ">>";
                pro.DisplayLinkToNextPage = true;
                pro.LinkToPreviousPageFormat = "<<";
                pro.DisplayLinkToPreviousPage = true;

                ViewBag.Pro = pro;
                ViewBag.DateTime = DateTime.Now.ToString();

                return View(banners.ToPagedList(pageNumber, pageSize));
            }
        }

        // GET: /Banner/CreateBanner/
        [HttpGet]
        public ActionResult CreateBanner()
        {
            return View();
        }

        // POST: /Banner/CreateBanner/
        [HttpPost]
        public ActionResult CreateBanner(Banner banner, HttpPostedFileBase BannerImage)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Luu anh len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Banners"), System.IO.Path.GetFileName(BannerImage.FileName));
                    BannerImage.SaveAs(path);
                    // Luu anh vao database 
                    banner.BannerImage = BannerImage.FileName;
                    using (var db = new WineDbContext())
                    {
                        db.Entry(banner).State = EntityState.Added;
                        db.SaveChanges();
                        return RedirectToAction("ShowBanner", "Banner");
                    }
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(banner);
        }

        // GET: /Banner/EditBanner/
        [HttpGet]
        [OutputCache(CacheProfile = "EditBanner")]
        public ActionResult EditBanner(int id = 0)
        {
            Banner banner = null;
            using (var db = new WineDbContext())
            {
                banner = db.Banners.Find(id);
                if (banner == null)
                {
                    return HttpNotFound();
                }
            }

            return View(banner);
        }

        // POST: /Banner/EditBanner/
        [HttpPost]
        public ActionResult EditBanner(Banner banner, HttpPostedFileBase BannerImage)
        {
            try
            {
                // Lay ve duong dan anh cu
                banner.BannerImage = (new WineDbContext().Banners.Find(banner.BannerId)).BannerImage;
                // Neu update anh moi thi thuc hien khoi lenh trong than cau lenh if
                if (BannerImage != null)
                {
                    // Xoa anh cu tren server
                    FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/Banners/" + banner.BannerImage));
                    if (fileInfo != null)
                    {
                        fileInfo.Delete();
                    }
                    // Luu anh moi len server
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/Images/Banners"), System.IO.Path.GetFileName(BannerImage.FileName));
                    BannerImage.SaveAs(path);
                    // Luu duong dan anh vao database 
                    banner.BannerImage = BannerImage.FileName;
                }

                using (var db = new WineDbContext())
                {
                    db.Entry(banner).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("ShowBanner", "Banner");
                }

            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }

            return View(banner);
        }

        // GET: /Banner/DeleteBanner/
        public ActionResult DeleteBanner(int id)
        {
            Banner banner = null;
            using (var db = new WineDbContext())
            {
                banner = db.Banners.Find(id);
                if (banner == null)
                {
                    return HttpNotFound();
                }
            }
            return View(banner);
        }

        // POST: /Banner/DeleteBanner/
        [HttpPost, ActionName("DeleteBanner")]
        public ActionResult DeleteConfirmed(int id)
        {
            Banner banner = null;
            try
            {
                using (var db = new WineDbContext())
                {
                    banner = db.Banners.Find(id);
                    // Xoa anh tren server
                    FileInfo fileInfo = new FileInfo(Server.MapPath("~/Content/Images/Banners/" + banner.BannerImage));
                    if (fileInfo != null)
                    {
                        fileInfo.Delete();
                    }
                    db.Entry(banner).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("ShowBanner", "Banner");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes");
            }
            return View(banner);
        }

    }
}

